package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Usuario;
import controller.UsuarioDAO;

/**
 * Servlet implementation class CadastrarUsuario
 */
@WebServlet("/CadastrarUsuario")
public class CadastrarUsuario extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastrarUsuario() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		try{
			Usuario usuario = new Usuario();
			UsuarioDAO DAO = new UsuarioDAO();
			
			usuario.setNome(request.getParameter("nome"));
			usuario.setMatricula(Integer.parseInt(request.getParameter("matricula")));
			usuario.setTipo(Integer.parseInt(request.getParameter("tipo")));
			usuario.setEmail(request.getParameter("email"));
			usuario.setLogin(request.getParameter("login"));
			usuario.setSenha(request.getParameter("senha"));
			
			DAO.inserir(usuario);
			
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/usuario_view.jsp");
			rd.forward(request, response);
			
			}catch(Exception e){
				throw new RuntimeException(e);
				
			}finally{
				out.close();
			}
		}
		

}
